package com.xiehua.task.service.impl;

import com.xiehua.common.utils.StringUtils;
import com.xiehua.task.job.pool.Job;
import com.xiehua.task.persistence.rocks.RocksPool;
import com.xiehua.task.persistence.rocks.XeihuaPersitence;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.xiehua.task.dao.ListDao;
import com.xiehua.task.domain.ListDO;
import com.xiehua.task.service.ListService;



@Service
public class ListServiceImpl implements ListService {
	@Autowired
	private ListDao listDao;

    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public ListDO get(Integer id){
		return listDao.get(id);
	}
	
	@Override
	public Job.JobPage list(Map<String, Object> map) throws Exception{
        GenericObjectPool<XeihuaPersitence> pool = RocksPool.getInstance();
        XeihuaPersitence persitence = pool.borrowObject();
        //build query
		Job.Query query = new Job.Query();
        String  startTime = (String)map.get("startTime");
        String  endTime = (String)map.get("endTime");
        if(StringUtils.isEmpty(startTime)){
            query.setStartTime(LocalDateTime.now().minusDays(1));
        }else{
            query.setStartTime(LocalDateTime.parse(startTime,formatter));
        }
        if(StringUtils.isEmpty(endTime)){
            query.setEndTime(LocalDateTime.now());
        }else{
            query.setEndTime(LocalDateTime.parse(endTime,formatter));
        }
        Job.JobPage page = persitence.queryJob(query);
        pool.returnObject(persitence);
        return page;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return listDao.count(map);
	}
	
	@Override
	public int save(ListDO list){
		return listDao.save(list);
	}
	
	@Override
	public int update(ListDO list){
		return listDao.update(list);
	}
	
	@Override
	public int remove(Integer id){
		return listDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return listDao.batchRemove(ids);
	}
	
}
